using Microsoft.Practices.EnterpriseLibrary.Validation;
using PrismFactory.SoftwareFactory.Views.Properties;

namespace PrismFactory.SoftwareFactory.Views.Validators
{
    public class NamespaceValidator : Validator<string>
    {
        private readonly IdentifierValidator identifierValidator;

        public NamespaceValidator()
            : base(null, null)
        {
            identifierValidator = new IdentifierValidator();
        }

        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate == null)
            {
                LogValidationResult(validationResults, Resources.InvalidNamespace, currentTarget, key);
                return;
            }
            foreach (string part in objectToValidate.Split('.'))
            {
                ValidationResults tempValidationResults = identifierValidator.Validate(part);
                if (!tempValidationResults.IsValid)
                {
                    LogValidationResult(validationResults, Resources.InvalidNamespace, currentTarget, key);
                    break;
                }
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return "The value {0} is not a valid namespace"; }
        }
    }
}